package com.nsoft.html5npub;

import com.nsoft.html5npub.business.PreferenceLoader;
import com.nsoft.html5npub.utils.Constant;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.view.Window;

@SuppressWarnings("deprecation")
public class SettingActivity extends PreferenceActivity 
 			implements OnSharedPreferenceChangeListener{

	EditTextPreference editPrefSvrName,editPrefAppName,
					editPrefBookUrl,editPrefAppId,
					editPrefIconUrl;
	CheckBoxPreference chkPrefUseAppId;
	ListPreference listPrefUIList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		
		addPreferencesFromResource(R.layout.preferences_view);
		getPreferenceScreen().getSharedPreferences().
			registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		Resources res=this.getResources();
		String keyConf=res.getString(
					R.string.key_server_config);
		SharedPreferences sp=this.getSharedPreferences(
					keyConf,Context.MODE_PRIVATE);
		
		String keyUIList=res.getString(
					R.string.key_ui_list);
		listPrefUIList=(ListPreference)
				findPreference(keyUIList);
		listPrefUIList.setSummary(sp.getString(
					keyUIList,Constant.EMPTY));
		
		String keySvrName=res.getString(
						R.string.key_server_name);
		editPrefSvrName=(EditTextPreference)
				findPreference(keySvrName);
		editPrefSvrName.setSummary(sp.getString(
						keySvrName,Constant.EMPTY));
		editPrefSvrName.setText(sp.getString(
						keySvrName,Constant.EMPTY));
		
		String keyAppName=res.getString(
						R.string.key_app_name);
		editPrefAppName=(EditTextPreference)
				findPreference(keyAppName);
		editPrefAppName.setSummary(sp.getString(
						keyAppName,Constant.EMPTY));
		editPrefAppName.setEnabled(false);
		
		String keyBookUrl=res.getString(
						R.string.key_book_url);
		editPrefBookUrl=(EditTextPreference)
				findPreference(keyBookUrl);
		editPrefBookUrl.setSummary(sp.getString(
						keyBookUrl,Constant.EMPTY));
		editPrefBookUrl.setEnabled(false);
		
		String keyUseAppId=res.getString(
						R.string.key_use_appid);
		chkPrefUseAppId=(CheckBoxPreference)
				findPreference(keyUseAppId);
		chkPrefUseAppId.setChecked(sp.getBoolean(
					keyUseAppId,Boolean.FALSE));
		chkPrefUseAppId.setOnPreferenceClickListener(
				new Preference.OnPreferenceClickListener() {
			
			@Override
			public boolean onPreferenceClick(
							Preference preference) {
				
				editPrefAppId.setEnabled(
						chkPrefUseAppId.isChecked());
				
				return false;
			}
		});
		
		
		String keyAppId=res.getString(
						R.string.key_appid);
		editPrefAppId=(EditTextPreference)
					findPreference(keyAppId);
		editPrefAppId.setSummary(sp.getString(
					keyAppId,Constant.EMPTY));
		editPrefAppId.setText(sp.getString(
						keyAppId,Constant.EMPTY));
		editPrefAppId.setEnabled(
				chkPrefUseAppId.isChecked());
		
		String keyIconUrl=res.getString(
						R.string.key_icon_url);
		editPrefIconUrl=(EditTextPreference)
				findPreference(keyIconUrl);
		editPrefIconUrl.setSummary(sp.getString(
						keyIconUrl,Constant.EMPTY));
		editPrefIconUrl.setEnabled(false);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences().
			unregisterOnSharedPreferenceChangeListener(this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		Resources res=this.getResources();
		SharedPreferences.Editor editor=
				this.getSharedPreferences(
					res.getString(
						R.string.key_server_config),
					Context.MODE_PRIVATE).edit();
		
		editor.clear();
		
		editor.putString(
				res.getString(R.string.key_ui_list),
				(String)listPrefUIList.getSummary());
		
		editor.putString(
				res.getString(R.string.key_server_name),
				(String)editPrefSvrName.getSummary());

		editor.putString(
				res.getString(R.string.key_app_name),
				(String)editPrefAppName.getSummary());

		editor.putString(
				res.getString(R.string.key_book_url),
				(String)editPrefBookUrl.getSummary());

		editor.putBoolean(
				res.getString(R.string.key_use_appid),
				(Boolean)chkPrefUseAppId.isChecked());
		
		editor.putString(
				res.getString(R.string.key_appid),
				(String)editPrefAppId.getSummary());
		
		editor.putString(
				res.getString(R.string.key_icon_url),
				(String)editPrefIconUrl.getSummary());

		editor.commit();
		
		PreferenceLoader.loadSettings(this);
	}
	
	@Override
	public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences,
				String key) {
		Preference pref=findPreference(key);
		if (pref instanceof EditTextPreference){
			((EditTextPreference)pref).setSummary(
					sharedPreferences.getString(
							key,Constant.EMPTY));
		}else if (pref instanceof CheckBoxPreference){
			((CheckBoxPreference)pref).setChecked(
					sharedPreferences.getBoolean(
							key,Boolean.FALSE));
		}else if (pref instanceof ListPreference){
			((ListPreference)pref).setSummary(
					sharedPreferences.getString(
							key,Constant.EMPTY));
		}
	}
	
}
